import 'package:flutter/cupertino.dart';
import 'package:flutter_danmu_anim/http/api.dart';
import 'package:flutter_danmu_anim/http/init.dart';
import 'package:flutter_danmu_anim/model/common/reply_type.dart';
import 'package:flutter_danmu_anim/model/model_hot_video_item.dart';
import 'package:flutter_danmu_anim/model/model_rec_video_item.dart';
import 'package:flutter_danmu_anim/model/model_response.dart';
import 'package:flutter_danmu_anim/model/user/fav_folder.dart';
import 'package:flutter_danmu_anim/model/video_detail_res.dart';

class VideoHttp {
  // 首页推荐视频
  static Future<ResponseListData<RecVideoItemModel>> rcmdVideoList({
    required int ps,
    required int freshIdx,
  }) =>
      tryCatch(() async {
        var res = await Request().get(
          Api.recommendList,
          data: {
            'feed_version': 'V3',
            'ps': ps,
            'fresh_idx': freshIdx,
          },
        );
        if (res.data['code'] == 0) {
          List<RecVideoItemModel> list = [];
          for (var i in res.data['data']['item']) {
            list.add(RecVideoItemModel.fromJson(i));
          }
          return ResponseListData<RecVideoItemModel>(status: true, data: list);
        } else {
          return ResponseListData<RecVideoItemModel>(
              status: false, data: [], msg: '');
        }
      });

  // 最热视频
  static Future<ResponseListData<HotVideoItemModel>> hotVideoList({
    required int pn,
    required int ps,
  }) =>
      tryCatch(() async {
        var res = await Request().get(
          Api.hotList,
          data: {
            'pn': pn,
            'ps': ps,
          },
        );
        if (res.data['code'] == 0) {
          List<HotVideoItemModel> list = [];
          for (var i in res.data['data']['list']) {
            list.add(HotVideoItemModel.fromJson(i));
          }
          return ResponseListData(status: true, data: list);
        } else {
          return ResponseListData(status: false, data: []);
        }
      });

  // 视频信息 标题、简介
  static Future<ResponseData<VideoDetailData>> videoIntro({
    required String aid,
  }) =>
      tryCatch(() async {
        var res = await Request().get(Api.videoIntro, data: {'aid': aid});
        VideoDetailResponse result = VideoDetailResponse.fromJson(res.data);
        if (result.code == 0) {
          return ResponseData(status: true, data: result.data);
        } else {
          Map errMap = {
            -400: '请求错误',
            -403: '权限不足',
            -404: '无视频',
            62002: '稿件不可见',
            62004: '稿件审核中',
          };
          return ResponseData(
              status: false,
              data: result.data,
              msg: errMap[result.code] ?? '请求异常');
        }
      });

  static Future<ResponseListData<HotVideoItemModel>> relatedVideoList({
    required String aid,
  }) =>
      tryCatch(() async {
        var res = await Request().get(Api.relatedList, data: {'aid': aid});
        if (res.data['code'] == 0) {
          List<HotVideoItemModel> list = [];
          for (var i in res.data['data']) {
            list.add(HotVideoItemModel.fromJson(i));
          }
          return ResponseListData(
            status: true,
            data: list,
          );
        } else {
          return ResponseListData(
            status: false,
            data: [],
            msg: "",
          );
        }
      });

  // 获取点赞状态
  static Future hasLikeVideo({required String aid}) async {
    var res = await Request().get(Api.hasLikeVideo, data: {'aid': aid});
    if (res.data['code'] == 0) {
      return {'status': true, 'data': res.data['data']};
    } else {
      return {'status': false, 'data': []};
    }
  }

  // 获取投币状态
  static Future hasCoinVideo({required String aid}) async {
    var res = await Request().get(Api.hasCoinVideo, data: {'aid': aid});
    if (res.data['code'] == 0) {
      return {'status': true, 'data': res.data['data']};
    } else {
      return {'status': true, 'data': []};
    }
  }

  // 获取收藏状态
  static Future hasFavVideo({required String aid}) async {
    var res = await Request().get(Api.hasFavVideo, data: {'aid': aid});
    if (res.data['code'] == 0) {
      return {'status': true, 'data': res.data['data']};
    } else {
      return {'status': false, 'data': []};
    }
  }

  // 一键三连
  static Future oneThree({required String aid}) async {
    var res = await Request().post(
      Api.oneThree,
      queryParameters: {
        'aid': aid,
        'csrf': await Request.getCsrf(),
      },
    );
    if (res.data['code'] == 0) {
      return {'status': true, 'data': res.data['data']};
    } else {
      return {'status': false, 'data': [], 'msg': res.data['message']};
    }
  }

  // （取消）点赞
  static Future likeVideo({required String aid, required bool type}) async {
    var res = await Request().post(
      Api.likeVideo,
      queryParameters: {
        'aid': aid,
        'like': type ? 1 : 2,
        'csrf': await Request.getCsrf(),
      },
    );
    if (res.data['code'] == 0) {
      return {'status': true, 'data': res.data['data']};
    } else {
      return {'status': false, 'data': [], 'msg': res.data['message']};
    }
  }

  // （取消）收藏
  static Future favVideo(
      {required String aid, String? addIds, String? delIds}) async {
    var res = await Request().post(Api.favVideo, queryParameters: {
      'rid': aid,
      'type': 2,
      'add_media_ids': addIds ?? '',
      'del_media_ids': delIds ?? '',
      'csrf': await Request.getCsrf(),
    });
    if (res.data['code'] == 0) {
      return {'status': true, 'data': res.data['data']};
    } else {
      return {'status': false, 'data': []};
    }
  }

  // 查看视频被收藏在哪个文件夹
  static Future videoInFolder({required int mid, required String rid}) async {
    var res = await Request()
        .get(Api.videoInFolder, data: {'up_mid': mid, 'rid': rid});
    if (res.data['code'] == 0) {
      FavFolderData data = FavFolderData.fromJson(res.data['data']);
      return {'status': true, 'data': data};
    } else {
      return {'status': false, 'data': []};
    }
  }

  // 发表评论 replyAdd

  // type	num	评论区类型代码	必要	类型代码见表
  // oid	num	目标评论区id	必要
  // root	num	根评论rpid	非必要	二级评论以上使用
  // parent	num	父评论rpid	非必要	二级评论同根评论id 大于二级评论为要回复的评论id
  // message	str	发送评论内容	必要	最大1000字符
  // plat	num	发送平台标识	非必要	1：web端 2：安卓客户端  3：ios客户端  4：wp客户端
  static Future replyAdd({
    required ReplyType type,
    required int oid,
    required String message,
    int? root,
    int? parent,
  }) async {
    if (message == '') {
      return {'status': false, 'data': [], 'msg': '请输入评论内容'};
    }
    debugPrint('root:$root');
    debugPrint('parent: $parent');

    var res = await Request().post(Api.replyAdd, queryParameters: {
      'type': type.index,
      'oid': oid,
      'root': root == null || root == 0 ? '' : root,
      'parent': parent == null || parent == 0 ? '' : parent,
      'message': message,
      'csrf': await Request.getCsrf(),
    });
    debugPrint(res.toString());
    if (res.data['code'] == 0) {
      return {'status': true, 'data': res.data['data']};
    } else {
      return {'status': false, 'data': []};
    }
  }

  // 查询是否关注up
  static Future hasFollow({required int mid}) async {
    var res = await Request().get(Api.hasFollow, data: {'fid': mid});
    if (res.data['code'] == 0) {
      return {'status': true, 'data': res.data['data']};
    } else {
      return {'status': true, 'data': []};
    }
  }

  // 操作用户关系
  static Future relationMod(
      {required int mid, required int act, required int reSrc}) async {
    var res = await Request().post(Api.relationMod, queryParameters: {
      'fid': mid,
      'act': act,
      're_src': reSrc,
      'csrf': await Request.getCsrf(),
    });
    if (res.data['code'] == 0) {
      return {'status': true, 'data': res.data['data']};
    } else {
      return {'status': true, 'data': []};
    }
  }
}
